<?php
/**
 * @Author: xuzhiyi
 * @Date:   2016-02-23 00:06:06
 * @Last Modified by:   xuzhiyi
 * @Last Modified time: 2016-03-04 11:29:30
 * ------------------------------------------------ *
 * 购物车                                          *
 * ------------------------------------------------ */
namespace Home\Controller;

class CartController extends CommonController {

// 跳到购物车列表

    public function cartList(){
   		$m = D('Cart');
		$cartInfo = $m->getCartInfo();
   		$this->assign('cartInfo',$cartInfo);
   		$this->display();

    }
    

//添加商品到购物车

	public function addToCart(){		
   		$m = D('Home/Cart');  		
   		$res = $m->addToCart();
   		$this->ajaxReturn($res);
    }

//修改购物车商品
//

    public function changeCartGoods(){
    	$m = D('Home/Cart');
   		$res = $m->addToCart();
   		$this->ajaxReturn($res);
    }
    

// 获取购物车信息
// 

	public function getCartInfo() {
		$m = D('Home/Cart');
		$cartInfo = $m->getCartInfo();
		$axm = (int)I("ajax",0);
		if($ajax ==1){
			echo json_encode($cartInfo);
		}else{
			$this->assign('cartInfo',$cartInfo);
			$this->display('List');
		}
		
	}
	

// 获取购物车商品数量

	public function getCartGoodCnt(){
		$shopcart = session("cart")?session("cart"):array();
		echo json_encode(array("goodscnt"=>count($shopcart)));
	}
    

// 检测购物车中商品库存
// 

	public function checkCartGoodsStock(){
		$m = D('Cart');
		$res = $m->checkCatGoodsStock();
		echo json_encode($res);

	}
	
	
	

// 删除购物车中的商品
// 

	public function delCartGoods(){	
		//dump($_SESSION);
		$m = D('Cart');	
		$res = $m->delCartGoods();
		$this->ajaxReturn($res);
		
	}
	

// 修改购物车中的商品数量
// 

	public function changeCartGoodsNum(){
			
		$m = D('Cart');
		$res = $m->changeCartGoodsnum();
		$cartInfo = $m->getCartInfo();
		$shopId=I('shopId');
		$cartInfo['shopTotalMoney']=$cartInfo['cartgoods'][$shopId]['totalMoney'];
		if($cartInfo['cartgoods'][$shopId]["totalMoney"]<$cartInfo['cartgoods'][$shopId]["deliveryFreeMoney"]){
			$cartInfo['deliveryMoney']=$cartInfo['cartgoods'][$shopId]["deliveryMoney"];
		}else{
			$cartInfo['deliveryMoney']=0;
		}
		unset($cartInfo['cartgoods']);
		//dump($cartInfo);
		$this->ajaxReturn($cartInfo);
		
	}
	

//去购物车结算
// 

	public function toCatpaylist(){	
		$m = D('Home/Cart');
		$cartInfo = $goodsmodel->getCartInfo();
		$this->assign("cartInfo",$cartInfo);
		
		$this->display('List');
	}
	
}